package com.netease.xone.fragment;

import preferMgr.XonePrefHelper;
import protocol.XoneService;
import protocol.meta.LoopBack;
import android.os.Bundle;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

import com.netease.pkgRelated.XoneConsts;
import com.netease.xone.R;


public class FragSetPicQuality extends FragmentBase {
    private RadioGroup mRadioGroup;
    private CheckBox mCheckBox;
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        setHasOptionsMenu(true);
        getActivity().setTitle(R.string.setting_item_picquality);
        View view = inflater.inflate(R.layout.fragment_pic_quality, container, false);
        findViews(view);
        return view;
    }
    private void findViews(View view){
        mRadioGroup = (RadioGroup)view.findViewById(R.id.pic_quality_group);
        RadioButton btn = (RadioButton)view.findViewById(R.id.quality_high);
        btn.setText(Html.fromHtml(getString(R.string.picquality_set_high_tip)));
        btn = (RadioButton)view.findViewById(R.id.quality_low);
        btn.setText(Html.fromHtml(getString(R.string.picquality_set_low_tip)));
        
        mCheckBox = (CheckBox)view.findViewById(R.id.pic_quality_check);
        mCheckBox.setChecked(XonePrefHelper.getPicHighWhenWifi(getActivity()));
        mCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked) {
                    XonePrefHelper.putPicHighWhenWifi(getActivity(), true);
                } else {
                    XonePrefHelper.putPicHighWhenWifi(getActivity(), false);
                } 
            }
        });
        int id = R.id.quality_high;
        int check = XonePrefHelper.getPicQuality(getActivity());
        switch(check){
            case XoneConsts.PIC_QUALTY_TYPE.high:
                id = R.id.quality_high;
                break;
            case XoneConsts.PIC_QUALTY_TYPE.normal:
                id = R.id.quality_normal;
                break;
            case XoneConsts.PIC_QUALTY_TYPE.low:
                id = R.id.quality_low;
                break;
        }
        mRadioGroup.check(id);
        mRadioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch(checkedId){
                    case R.id.quality_high:
                        XonePrefHelper.putPicQuality(getActivity(), XoneConsts.PIC_QUALTY_TYPE.high);
                        break;
                    case R.id.quality_normal:
                        XonePrefHelper.putPicQuality(getActivity(), XoneConsts.PIC_QUALTY_TYPE.normal);
                        break;
                    case R.id.quality_low:
                        XonePrefHelper.putPicQuality(getActivity(), XoneConsts.PIC_QUALTY_TYPE.low);
                        break;
                }
               LoopBack lb = new LoopBack();
               lb.mType = XoneConsts.LOOPBACK_TYPE.pic_quality_set_changed;
               XoneService.getInstance().doLoopBack(lb);
            }
        });
    }
}
